//
//  AFNDownloadImgViewController.m
//  oc_demo
//
//  Created by haigui on 2021/10/9.
//

#import "AFNDownloadImgViewController.h"
#import "MMButton.h"
#import "MMNetworkRequest.h"
#import <SVProgressHUD/SVProgressHUD.h>
@interface AFNDownloadImgViewController ()

@property (nonatomic, strong) UIImageView *downloadImgView;

@property (nonatomic, strong) NSString *savePath;
@end

@implementation AFNDownloadImgViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下载" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonItemClick:)];
    
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *mapPath = [path stringByAppendingPathComponent:@"hall_map"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = NO;
    BOOL isExit = [fileManager fileExistsAtPath:mapPath isDirectory:&isDir];
    if (!isDir && !isExit) {
        [fileManager createDirectoryAtPath:mapPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    _savePath = [mapPath stringByAppendingPathComponent:@"hall_map_1.jpg"];
    if (![fileManager fileExistsAtPath:_savePath]) {
        if ([UIImageJPEGRepresentation([UIImage imageNamed:@"hall_map_1.jpg"], 1.0) writeToFile:_savePath atomically:YES]) {
            NSLog(@"写入成功");
        };
    }

    [self.view addSubview:self.downloadImgView];
    
    if ([fileManager fileExistsAtPath:_savePath]) {
        [self setDownloadImg:_savePath];
    }
//    [self setDownloadImg:[[NSBundle mainBundle] pathForResource:@"hall_map_1" ofType:@"jpg"]];
    
}
- (void)rightBarButtonItemClick:(UIBarButtonItem *)barBtn{
    
    NSString *urlStr = @"https://t7.baidu.com/it/u=4198287529,2774471735&fm=193&f=GIF";
    
    [[MMNetworkRequest sharedNetworkingManage] DOWNLOAD:urlStr progress:^(double progress) {
        [SVProgressHUD showProgress:progress];
    } success:^(id  _Nonnull responseObject) {
        [self hideWaitHUD];
        
        if ([UIImageJPEGRepresentation([UIImage imageWithContentsOfFile:responseObject], 1.0) writeToFile:_savePath atomically:YES]) {
            [self setDownloadImg:_savePath];
        };
                
    } failure:^(NSError * _Nonnull error) {
        [self showErrorWithMsg:@"下载失败"];
    }];
}

- (void)setDownloadImg:(NSString *)imagePath{
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
    if (image) {
        _downloadImgView.image = image;
        _downloadImgView.height = _downloadImgView.width/_downloadImgView.image.size.width*_downloadImgView.image.size.height;
    }
}

- (UIImageView *)downloadImgView{
    if (!_downloadImgView) {
        _downloadImgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH-20, 0)];
        _downloadImgView.contentMode = UIViewContentModeScaleAspectFill;
        _downloadImgView.clipsToBounds = YES;
    }
    return _downloadImgView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
